package cn.dhn_app.horseman_app.service.impl;

import cn.dhn_app.horseman_app.service.HorsemanCreateAccountService;
import cn.dhn_app.util.tokenTool;
import cn.itcast.mapper.horsemanMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.util.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 10:06 2020/8/25
 * @Modified By:
 */
@Service
@Slf4j
public class HorsemanCreateAccountServiceImpl implements HorsemanCreateAccountService {

    @Resource
    private horsemanMapper horsemanMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     *
     * @Description: App骑手端注册
     *
     * @auther: 张松
     * @date: 10:32 2020/8/25
     * @param: [horseman]
     * @return: cn.itcast.util.ResponseEntity
     *
     */
    @Override
    @Transactional
    public ResponseEntity horsemanCreateAccount(horseman horseman, Integer phoneCode) {
        String horsemanPhone = horseman.getHorsemanPhone();
        List<horseman> horsemansPhone = horsemanMapper.selectHorsemanphone(horsemanPhone);
        List<horseman> horsemenNumber = horsemanMapper.selectHorsemanNumber(horseman.getHorsemanNumber());
        String rediscode = stringRedisTemplate.opsForValue().get(horseman.getHorsemanPhone()+"code");
        if (!rediscode.equals(String.valueOf(phoneCode))){
            return new ResponseEntity(302,"验证码错误");
        }else if(rediscode==null){
            return new ResponseEntity(304,"验证码为空");
        }
        try{
            if (!horsemansPhone.isEmpty()||horsemansPhone.size()>0||!horsemenNumber.isEmpty()||horsemenNumber.size()>0){
                return new ResponseEntity(300,"账号已注册");
            }
             horseman.setOpenId(new tokenTool().makeToken());
            int i = horsemanMapper.insertSelective(horseman);
            System.out.println(i);
            if (i>0){
                stringRedisTemplate.opsForValue().set(horseman.getHorsemanPhone()+"code",String.valueOf(phoneCode+phoneCode+phoneCode));
                log.info(horseman.getHorsemanName()+"注册成功");
                return new ResponseEntity(200,"账号注册成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseEntity(400,"账号注册失败,请重新注册");
    }

}
